﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Bortosky.Google.Visualization;
using Icoders.MarketTracker.Reports.Lib.Repository.Transactions;
using Icoders.MarketTracker.Reports.Lib.Util;
using Icoders.MarketTracker.Web.Lib.Security;
using Icoders.MarketTracker.Web.Lib.Util.Helper;

namespace Icoders.MarketTracker.Web.Controllers
{
     [CustomAuthorize]
    public class DashBoardController : Controller
    {
        //
        // GET: /DashBoard/
        private IDashboardReportService _dashboardReportService;

        public DashBoardController(IDashboardReportService dashboardReportService)
        {
            _dashboardReportService = dashboardReportService;
        }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult SalesVolume()
        {
            return View();
        }
        public ActionResult SalesValue()
        {
            return View();
        }
        [HttpPost]
        public ActionResult GetPenetrationDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetPenetrationDashBoard(Common.GetClientIdFromCookies()).ToList().ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }

        [HttpPost]
        public ActionResult GetSalesValuePerPeriodDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetSalesValuePerPeriodDashBoard(Common.GetClientIdFromCookies()).ToList().ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }
        [HttpPost]
        public ActionResult GetSalesValuePerAreaDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetSalesValuePerAreaDashBoard(Common.GetClientIdFromCookies()).ToList().ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }
        [HttpPost]
        public ActionResult GetSalesValueAvgPerPeriodDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetSalesValueAvgPerPeriodDashBoard(Common.GetClientIdFromCookies()).ToList().ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }
        [HttpPost]
        public ActionResult GetTopSellingProductDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetSalePerSkuDashBoard(Common.GetClientIdFromCookies()).ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }
        [HttpPost]
        public ActionResult GetTopSellingFSRDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetSalePerFsrDashBoard(Common.GetClientIdFromCookies()).ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }
        public ActionResult GetBottomSellingFsrDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetBottomSalePerFsrDashBoard(Common.GetClientIdFromCookies()).ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }
        [HttpPost]
        public ActionResult GetBottomSalesValuePerAreaDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetBottomSalesValuePerAreaDashBoard(Common.GetClientIdFromCookies()).ToList().ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }
        [HttpPost]
        public ActionResult GetProductAvalibiltyDashBoard()
        {
            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetProductAvalibiltyDashBoard(Common.GetClientIdFromCookies()).ToList().ToDataTable();
            result.Content = new GoogleDataTable(data).GetJson();
            return result;

        }
        [HttpPost]
        public ActionResult GetSaleByBrandChartData()
        {

            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetSalePerBrandPerPeriodDashBoard(Common.GetClientIdFromCookies()).ToList().ToDataTable();
            DataTable data2 = PivotHelper.Pivot(data, "Period", "Product", "Quantity");

            result.Content = new GoogleDataTable(data2).GetJson();

            return result;

        }
    }
}
